package com.mark.web.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.mark.common.Constant;
import com.mark.common.ServerResponse;
import com.mark.web.sys.entity.po.Department;
import com.mark.web.sys.entity.po.User;
import com.mark.web.sys.entity.vo.DeptVo;
import com.mark.web.sys.mapper.DepartmentMapper;
import com.mark.web.sys.mapper.UserMapper;
import com.mark.web.sys.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 系统的部门的业务实现
 * @Author: Mark
 * @CreateDate: 2019/1/13 14:59
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Service
public class DeptServiceImpl implements IDeptService {
    @Autowired
    private  DepartmentMapper departmentMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 新增部门
     * @param department ：部门信息
     * @return ;
     */
    @Override
    public ServerResponse<String> addDept(Department department) {
        ServerResponse<String> validateRes = validate(department,department.getId());
        if(!validateRes.isSuccess()){
            return validateRes;
        }
        int result = departmentMapper.insert(department);
        if(result == Constant.SUCCESS){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorMessage("保存部门数据失败！");
    }

    /**
     * 更新部门信息
     * @param department ：待更新的部门信息
     * @return ：
     */
    @Override
    public ServerResponse<String> editDept(Department department) {
        ServerResponse<String> validateRes = validate(department,department.getId());
        if(!validateRes.isSuccess()){
            return validateRes;
        }
        int result = departmentMapper.updateByPrimaryKeySelective(department);
        if(result == Constant.SUCCESS){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorMessage("保存更新部门数据失败！");
    }

    /**
     * 分页获取部门信息
     * @param pageNum   ：页数
     * @param pageSize  ：页面大小
     * @return ：
     */
    @Override
    public ServerResponse<PageInfo<DeptVo>> getDeptList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Department> departmentList = departmentMapper.selectDeptList();
        List<DeptVo> deptVoList = Lists.newArrayList();
        for(Department department : departmentList){
            DeptVo deptVo = new DeptVo(department);
            deptVoList.add(deptVo);
        }
        PageInfo<DeptVo> departmentPageInfo = new PageInfo<>(deptVoList);
        return ServerResponse.createBySuccess(departmentPageInfo);
    }

    @Override
    public ServerResponse<DeptVo> getDept(Integer id) {
        Department department = departmentMapper.selectByPrimaryKey(id);
        if(department == null){
            return ServerResponse.createByErrorMessage("用户信息不存在！");
        }
        DeptVo deptVo = new DeptVo(department);
        return ServerResponse.createBySuccess(deptVo);
    }

    /*
     * ------------------------------- private methods---------------------------------------------
     */
    /**
     * 数据校验
     * @param department ：新增或更新的部门信息，
     * @param id ：部门ID，新增的时候为null，更新的时候为具体的值
     * @return ：
     */
    private ServerResponse<String> validate(Department department, Integer id) {
        //校验负责人是否存在
        User leader = userMapper.selectByPrimaryKey(department.getLeaderId());
        if(leader == null){
            return ServerResponse.createByIllegalArgument("输入的领导人ID相关信息不存在！");
        }
        //校验部门名称是已经存在
        int effectNum = departmentMapper.validateDeptName(id, department.getName());
        if(effectNum >= Constant.SUCCESS){
            return ServerResponse.createByIllegalArgument("该部门名称已经存在！");
        }
        return ServerResponse.createBySuccess();
    }

}
